package fun.boomcat.luckyhe.mirai.plugin.utils;

import fun.boomcat.luckyhe.mirai.plugin.config.RconCommandConfigOperation;
import fun.boomcat.luckyhe.mirai.plugin.pojo.CommandMapping;
import fun.boomcat.luckyhe.mirai.plugin.service.CommandMappingService;
import fun.boomcat.luckyhe.mirai.plugin.service.CommandMappingServiceImpl;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

public class CommandMappingWebThread implements Runnable{
    private static ServerSocket serverSocket;
    private static String outputString = "";

    static {
        boolean flag = true;
        while (flag) {
            try {
                int port = Integer.parseInt(RconCommandConfigOperation.getWebsiteInfo().get("port"));
                serverSocket = new ServerSocket(port);
                flag = false;
            } catch (Exception ignored) {

            }
        }

        Thread commandMappingWebThread = new Thread(new CommandMappingWebThread());
        commandMappingWebThread.setDaemon(true);
        commandMappingWebThread.start();
    }

    public static String httpFrame(String title, List<CommandMapping> commandMappings) {

        StringBuilder sb = new StringBuilder();
        sb.append("HTTP/1.1 200 OK\n");
        sb.append("Content‐Type:text/html\n\n");
        sb.append("<!DOCTYPE html>");

        sb.append("<html>");
        sb.append("<head><title>查询结果</title></head>");

        sb.append("<body>");

        sb.append("<h1 align=\"center\">").append(title).append("</h1>");
        sb.append("<table align=\"center\" cellpadding=\"5\"style=\"border: 1px solid black;\">");
        sb.append("<tr>").append("<th>qq</th>").append("<th>玩家指令</th>").append("<th>实际执行指令</th>")
                .append("<th>指令描述</th>").append("</tr>");

        for (CommandMapping commandMapping : commandMappings) {
            sb.append("<tr>");

            sb.append("<td>").append(commandMapping.getQq()).append("</td>");
            sb.append("<td>").append(commandMapping.getSource()).append("</td>");
            sb.append("<td>").append(commandMapping.getMapping()).append("</td>");
            sb.append("<td>").append(commandMapping.getDescription()).append("</td>");
            sb.append("</tr>");
        }

        if (commandMappings.size() == 0) {
            sb.append("<h1 align=\"center\">").append("无结果").append("</h1>");
        }

        sb.append("</table>");
        sb.append("</body>");
        sb.append("</html>");

        return sb.toString();
    }

    public static void showAllCommandMappings() throws IOException {
        CommandMappingService service = new CommandMappingServiceImpl();
        List<CommandMapping> commandMappings = service.selectAllCommandMapping();

        outputString = httpFrame("所有指令映射", commandMappings);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Socket socket = serverSocket.accept();

                BufferedWriter bw = new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream(), "GBK")
                );
                bw.write(outputString);
                bw.flush();
                bw.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
